package com.example.mq.demo;

import com.example.mq.client.Channel;
import com.example.mq.client.Connection;
import com.example.mq.client.ConnectionFactory;
import com.example.mq.server.core.ExchangeType;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class DemoProducer {
    public static void main(String[] args) throws IOException, InterruptedException {
        System.out.println("启动生产者");
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("127.0.0.1");
        factory.setPort(9090);

        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        // 创建交换机和队列
        channel.exchangeDeclare("testExchange", ExchangeType.DIRECT, true, false, null);
        channel.queueDeclare("testQueue", true, false, false, null);

        // 创建消息并发送
        for (int i = 0; i < 10; i++) {
            byte[] body = "hello".getBytes(StandardCharsets.UTF_8);
            channel.basicPublish("testExchange", "testQueue", null, body);
        }
        Thread.sleep(500);
        channel.close();
        connection.close();
    }
}
